﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using RTW.OA.Areas.Overseas.Servsers;
using RTW.OA.Models;
using RTW.OA.Controllers;

namespace RTW.OA.Areas.Overseas.Controllers
{
    public class TripsController : BaseController
    {
        private TripsServers tripsServers;

        public TripsController()
        {
            tripsServers = new TripsServers();
        }
        // GET: Overseas/Trips
        public ActionResult Trips()
        {
            return View();
        }

        // GET: Overseas/TripsEdit
        public ActionResult TripsEdit(int? id)
        {
            //ViewBag.LEADER_IDData = paibanServers.GetLEADER_ID();
            var model = new overseas_trips();
            if (id != null)
            {
                ViewBag.Title = "编辑因公出国";
                model = GetTripsModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建因公出国";
            }
            return View(model);
        }

        // GET: Overseas/TripsEdit
        [HttpPost]
        public ActionResult TripsEdit(int? id, overseas_trips model)
        {
            try
            {
                if (id == null || id == 0)
                {
                    model.CREATE_DATE = DateTime.Now;
                    model.ALLOW = "0";
                    tripsServers.Add(model);
                    ViewBag.msg = "新增出国信息成功！";
                    model = new overseas_trips();

                }
                else
                {
                    tripsServers.Update(model);
                    ViewBag.msg = "修改出国信息成功！";

                }
            }
            catch (Exception e)
            {
                string stre = e.Message;
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        // GET: Overseas/TripsDetails
        public ActionResult TripsDetails(int? id)
        {
            var model = new overseas_trips();
            if (id != null)
            {
                model = GetTripsModel(id.Value.ToString());
            }
            return View(model);
        }

        // GET: Overseas/TripsDelete
        public ActionResult TripsDelete(int id)
        {
            if (id != 0)
            {
                tripsServers.Del(id.ToString());
                ViewBag.msg = "删除出国信息成功！";
            }
            return Content("");
        }

        // GET: Overseas/TripsApproval
        public ActionResult TripsApproval(int id,string REASON,string ALLOW)
        {
            if (id != 0)
            {
                string strLEADER_ID = CurrentUser.USER_NAME.ToString();
                tripsServers.updateALLOW(id,REASON, strLEADER_ID,ALLOW);
                ViewBag.msg = "审批成功！";
            }
            return Content("");
        }

        public overseas_trips GetTripsModel(string id)
        {
            var model = tripsServers.GetModelById(id);
            return model;
        }

        public string GetTripsTableJsonResult()
        {
            var list = tripsServers.GetData();
            return JsonConvert.SerializeObject(list);
        }
    }
}